package com.ruoyi.personnel.oa.leave.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.oa.leave.domain.LeaveCond;
import com.ruoyi.personnel.oa.leave.domain.LeaveDomain;
import com.ruoyi.personnel.oa.leave.domain.LeaveDto;
import com.ruoyi.personnel.oa.leave.mapper.LeaveMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 请假
 * 业务逻辑层
 */
@Service
public class LeaveServiceImpl implements ILeaveService{
    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 根据搜索条件所有请假记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<LeaveDto> selectLeaveList(LeaveCond cond) {
        return leaveMapper.selectLeaveList(cond);
    }

    /**
     * 新增请假
     * @param leave
     * @return
     */
    @Override
    public int insertLeave(LeaveDomain leave) {
        if (StringUtils.isNotNull(leaveMapper.selectLeaveById(leave.getLeaveId()))) {
            //存在
            return 0;
        }
        //不存在
        leave.setLeaveId(idUtil.createLeaveId()); //主键添加
        leaveMapper.insertLeave(leave);
        return 1;
    }

    /**
     * 修改请假
     * @param leave
     * @return
     */
    @Override
    public int updateLeave(LeaveDomain leave) {
        if (!StringUtils.isNotNull(leaveMapper.selectLeaveById(leave.getLeaveId()))) {
            //不存在
            return 0;
        }
        //不存在
        leaveMapper.updateLeave(leave);
        return 1;
    }

    /**
     * 删除请假
     * 仅修改删除标志
     * @param leave
     * @return
     */
    @Override
    public int updateLeaveDeleteFlag(LeaveDomain leave) {
        return leaveMapper.updateLeaveDeleteFlag(leave);
    }
}
